<template>
	<view>

		<view class="top u-flex u-row-between">
			<view class="u-flex" style="align-items: center;">
				<u-image src="/static/loc_icon.png" width="60" height="60"></u-image>
				<view class="u-margin-left-20" style="width: 500rpx;" v-if="address.id">
					<view>
						<text style="font-size: 32rx;font-weight: bold;">{{address.realname}}</text>
						<text style="color:#999999;margin-left: 20rpx;">{{address.phone}}</text>
					</view>
					<view class="">
						{{address.address}}
					</view>
				</view>
				<view v-else style="font-size: 32rpx;font-weight: bold;margin-left: 20rpx;" hover-class="none">
					<navigator url="../address/addAddress">请先选择收货地址</navigator>
				</view>
			</view>
			<u-icon name="edit-pen" color="#037705" size="40" @click="changeAddress" v-if="address.id"></u-icon>
		</view>

		<view v-for="(item,index) in list" :key="index" class="list">
			<view class="u-flex">
				<u-image :src="item.cover" width="160" height="160"></u-image>
				<view class="right">
					<view class="title">
						{{item.goodsName}}
					</view>
					<view class="controls">
						<view class="price">
						    ￥{{item.price}}
						</view>
						<view class="num">
							x{{item.num}}
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="mode" >
			<u-cell-group>
				<u-cell-item title="商品运费" :value="expressFee+'元'" :arrow="false" :hover-class="false"></u-cell-item>
				<u-cell-item title="商品备注" :arrow="false" :hover-class="false">
					<view slot="label" style="width: 650rpx;">
						<u-input v-model="liuyan" :border="border" placeholder="在此处添加您的订单备注" type="textarea"/>
					</view>
				</u-cell-item>
			</u-cell-group>
		</view>


		<view class="bottomFix">
			<view class="u-flex" style="align-items: center;">
				<view class="u-flex u-margin-right-20">
					<view>含运费共：</view>
					<view style="color:#FF6450;font-size: 56rpx;">￥{{payMoney}}</view>
				</view>
				<view class="sure" @click="sure">立即支付</view>
			</view>

		</view>

		<u-toast ref="uToast" />
		<paystatus ref="children" @jisuan="jisuan"></paystatus>

	</view>
</template>

<script>
	import payway from '../../common/wxpay.js';
	export default {
		data() {
			return {
				liuyan: "",
				expressFee: '',
				address_id:"",
				payMoney: "",
				payway: "余额支付",
				conpund: "",
				address: {},
				list: [],
				num: '',
				id: '',
				Sid: '',
				switchVal: false,
				voucher: "",
				car_id: [],
				commission: 0,
				conpundMoney: 0,
				realprice: "",
				ptt: "",
				isjifen:"",
				goodsType: "", //goodsType=1 普通商品 //goodsType=2 积分商品 
			}
		},
		async onLoad(options) {
			console.log(options)
			uni.hideShareMenu()
			if (options.id) {
				this.num = options.num;
			
				this.id = options.id
			}

			if (options.car_id) {
				this.car_id = options.car_id.split(",")
			}

	
			await this.getOrder()

		},
		onShow() {
			this.getAddress()

		},
		methods: {
			changeAddress() {
				uni.navigateTo({
					url: "../address/index"
				})
			},
			// 获取地址信息
			getAddress() {
				this.$u.post('/api//user/address', {
address_id:this.address_id
				}, {}).then(res => {
					if(Array.isArray(res.data)){
						if (res.data.length) {
							this.address = res.data[0]
						}
					}else{
						  this.address = res.data
					}
				}).catch(err => {})

			},
			// 获取订单信息
			getOrder() {
				return new Promise((resolve, reject) => {
					this.$u.post('/api/indent/affirm', {
						goods_id: this.id,
						goods_spec_id: this.Sid,
						num: this.num,
						cart_id: this.car_id
					}, {}).then(res => {
					
						this.list = res.data.goods;
						
						var expressFreeMoney=this.$store.state.getConfig.expressFreeMoney;
						var expressMoney=this.$store.state.getConfig.expressMoney;
						if(Number(res.data.totalMoney)<Number(expressFreeMoney)){
							console.log((Number(res.data.totalMoney)+Number(expressMoney)),66)
							this.payMoney =(Number(res.data.totalMoney)+Number(expressMoney)).toFixed(2);
							this.expressFee=Number(expressMoney).toFixed(2);
						}else{
							this.payMoney =res.data.totalMoney
							this.expressFee="0.00";
						}
						
						
					
				
						resolve(res)

					}).catch(err => {})
				})


			},
			sure() {

				if (!this.address.id) {
					this.$refs.uToast.show({
						title: '请先填写收货地址',
						type: 'error',
					})
				} else {
					this.$u.post('/api/indent/buy', {
						address_id: this.address.id,
						cart_id: this.car_id,
						goods_id: this.id,
						num: this.num,		
					    use_balance:0
					}, {}).then(res => {
						if(res.data){
							var packages = res.data.package;
							var {
								noncestr,
								timestamp,
								signtype,
								paysign
							} = res.data
							payway.wx(timestamp, noncestr, packages, signtype, paysign).then(res => {
								uni.navigateTo({
									url: "../book/index"
								})
							
							}).catch(err => {
								uni.navigateBack({
							
								})
							})
						}else{
								uni.navigateTo({
									url: "../book/index"
								})
						}
						


					}).catch(err => {})


				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 200rpx;
	}

	.mode {

		background: #FFFFFF;
		box-shadow: 0px 4rpx 6rpx rgba(209, 209, 209, 0.26);
		border-radius: 20rpx;
		width: 690rpx;
		margin: 40rpx auto;
	}

	.top {
		width: 690rpx;
		background: #FFFFFF;
		box-shadow: 0px 4rpx 6rpx rgba(209, 209, 209, 0.26);
		border-radius: 20rpx;
		margin: 40rpx auto;
		padding: 30rpx;
		display: flex;
		justify-content: space-between;
	}

	.list {
		width: 690rpx;
		height: 240rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 6rpx rgba(209, 209, 209, 0.26);
		border-radius: 20rpx;
		margin: auto;
		padding: 20rpx;
		display: flex;
		align-items: center;

		.right {
			margin-left: 20rpx;
			width: 450rpx;

			.size {
				background: #F6F6F6;
				border-radius: 30rpx;
				padding: 7rpx 30rpx;
				text-align: center;
				display: table !important;
				font-size: 24rpx;
				color: #999999;
				margin: 20rpx 0;
			}
		}

		.controls {
			display: flex;
			justify-content: space-between;

			.price {
				color: #FF6450;
				font-size: 36rpx;
			}

			.num {
				color: #037705;
				font-size: 36rpx;

			}
		}



	}

	.bottomFix {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		display: flex;
		justify-content: flex-end;
		padding: 20rpx;
		align-items: center;
		box-shadow: 0px -2rpx 6rpx rgba(229, 229, 229, 0.4);
		background: #FFFFFF;
		z-index: 99;
	}

	.sure {
		width: 200rpx;
		height: 80rpx;
		background: #037705;
		border-radius: 40rpx;
		text-align: center;
		font-size: 28rpx;
		line-height: 80rpx;
		color: #fff;

	}
</style>
